package com.renwei.yunlong.activity;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.renwei.yunlong.R;
import com.renwei.yunlong.base.BaseActivity;
import com.renwei.yunlong.utils.DataCleanManager;
import com.zyf.fwms.commonlibrary.utils.AutoUtils;
import com.zyf.fwms.commonlibrary.utils.CommonUtils;
import com.zyf.fwms.commonlibrary.utils.StatusBarUtil;

import butterknife.Bind;
import butterknife.ButterKnife;

import static android.webkit.WebSettings.PluginState.ON;

/**
 * 设置--页面
 * <p>
 * Created by Administrator on 2017/8/25.
 */

public class SettingActivity extends BaseActivity implements View.OnClickListener {


    @Bind(R.id.iv_back)
    ImageView ivBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.btn_out_login)
    Button btnOutLogin;
    @Bind(R.id.rl_change)
    RelativeLayout rlChange;
    @Bind(R.id.rl_clear)
    RelativeLayout rlClear;
    @Bind(R.id.rl_feedback)
    RelativeLayout rlFeedback;
    @Bind(R.id.rl_protocol)
    RelativeLayout rlProtocol;
    @Bind(R.id.rl_about)
    RelativeLayout rlAbout;

    @Bind(R.id.tv_size)
    TextView tvSize;

    private DataCleanManager manager;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_setting);
        ButterKnife.bind(this);
        StatusBarUtil.setColor(this, CommonUtils.getColor(this, R.color.title_color), 0);

        //屏幕自适配
        AutoUtils.setSize(this, false, 720, 1280);
        AutoUtils.auto(this);

        initData();
    }

    private void initData() {
        ivBack.setVisibility(View.VISIBLE);
        tvTitle.setText("设置");
        ivBack.setOnClickListener(this);
        btnOutLogin.setOnClickListener(this);

        rlChange.setOnClickListener(this);
        rlClear.setOnClickListener(this);
        rlFeedback.setOnClickListener(this);
        rlProtocol.setOnClickListener(this);
        rlAbout.setOnClickListener(this);

        getSize();//获得数据容量
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.rl_change:
                startActivity(new Intent(this, ChangePasswordActivity.class));
                break;
            case R.id.rl_clear:
                showDialog();
                break;
            case R.id.rl_feedback://意见反馈
                startActivity(new Intent(this, FeedbackActivity.class));
                break;
            case R.id.rl_protocol:
                startActivity(new Intent(this, ProtocolActivity.class));
                break;
            case R.id.rl_about:
                startActivity(new Intent(this, AboutActivity.class));
                break;
            case R.id.btn_out_login:
                startActivity(new Intent(this, LoginActivity.class));
                SettingActivity.this.finish();
                break;

        }
    }

    //获得数据容量
    public void getSize() {
        try {
            final String size = manager.getFormatSize(manager.getFolderSize(getExternalFilesDir(null))
                    + manager.getFolderSize(getExternalCacheDir()));
            tvSize.setText(size);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //显示dialog
    private void showDialog() {

        View view = View.inflate(getApplicationContext(), R.layout.clear_dialog_layout, null);
        // 实例化 Dialog
        final Dialog dialog = new Dialog(SettingActivity.this);

        AutoUtils.auto(view);

        // 获取窗口管理器
        Window dialogWindow = dialog.getWindow();
        // 隐藏标题栏
        dialogWindow.requestFeature(Window.FEATURE_NO_TITLE);
        Button btnCancel = (Button) view.findViewById(R.id.btn_cancel);//取消按钮
        Button btnConfirm = (Button) view.findViewById(R.id.btn_confirm);//确认按钮
        // 设置显示的View
        dialogWindow.setContentView(view);
        // 设置显示的位置
        dialogWindow.setGravity(Gravity.CENTER);
        dialog.show();
        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DataCleanManager.cleanCache(SettingActivity.this);
                dialog.dismiss();
            }
        });
        btnConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              dialog.dismiss();
            }
        });
    }
}
